package cn.afactory.tygz.model;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.NoAutoIncrement;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.annotation.Transient;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import cn.afactory.tygz.common.ErrorCode;

/**
 * Created with Android Studio.
 * Title:UserInfo
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/9/8
 * Time:下午1:57
 * Version 1.0
 */
@Table(name="user_infos")
public class UserInfo extends BaseDataHeader implements Serializable{
    private static final long serialVersionUID = 7054790919159097603L;

    @Column(column="level")
    public int level;
    @Column(column="signature")
    public String signature;
    @Column(column="company")
    public String company;

    //所在地
    @Column(column="cityName")
    public String cityName;
    @Column(column="cityCode")
    public String cityCode;

    @Column(column="locationCityName")
    public String locationCityName;
    @Column(column="locationCityCode")
    public String locationCityCode;


    @Column(column="registerFrom")
    public int registerFrom;
    @Transient
    public String addUser;
    @Column(column="registerTime")
    public long registerTime;
    @Transient
    public long addTime;
    @Transient
    public int enabled;
    //用户名
    @Column(column="nickName")
    public String nickName;
    //0 表示男 1 表示女
    @Column(column="sex")
    public int sex;
    //金币
    @Column(column="money")
    public int money;
    //手机号
    @Column(column="telphone")
    public String telphone;
    //个人介绍
    @Column(column="intro")
    public String intro;
    //目的
    @Column(column="dest")
    public String dest;
    //粉丝数
    @Column(column="fans")
    public int fans;
    //关注数
    @Column(column="focus")
    public int focus;

    @Column(column="birth")
    public long birth;
    @Transient
    public String accessToken;
    //情感状态
    @Column(column="maritalStatus")
    public String maritalStatus;

    @Column(column="userIcon")
    public String userIcon;

    @Id
    @NoAutoIncrement
    @Column(column="id")
    public long id;
    @Transient
    public boolean isFocus;
    @Transient
    public boolean isBlack;

    public static UserInfo deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }
    public static UserInfo deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            UserInfo result = new UserInfo();
            result.message = json.optString("message");
            result.state = json.optInt("state");
            json = json.optJSONObject("data");
            if(result.state == ErrorCode.STATUS_OK) {
                result.accessToken = json.optString("accessToken");
                json = json.optJSONObject("value");
                if (json != null) {
                    result.addTime = json.optLong("addTime");

                    result.birth = json.optLong("birth");

                    result.id = json.optLong("userId");

                    result.sex = json.optInt("sex");

                    result.level = json.optInt("level");

                    result.money = json.optInt("integral");

                    result.fans = json.optInt("fans");

                    result.focus = json.optInt("focus");

                    if (!json.isNull("signature")) {
                        result.signature = json.optString("signature", null);
                    }

                    if (!json.isNull("userIcon")) {
                        result.userIcon = json.optString("userIcon", null);
                    }

                    if (!json.isNull("company")) {
                        result.company = json.optString("company", null);
                    }

                    if (!json.isNull("cityName")) {
                        result.cityName = json.optString("cityName", null);
                    }

                    if (!json.isNull("cityCode")) {
                        result.cityCode = json.optString("cityCode", null);
                    }

                    if (!json.isNull("locationCityName")) {
                        result.locationCityName = json.optString("locationCityName", null);
                    }

                    if (!json.isNull("locationCityCode")) {
                        result.locationCityCode = json.optString("locationCityCode", null);
                    }

                    if (!json.isNull("nickName")) {
                        result.nickName = json.optString("nickName", null);
                    }

                    if (!json.isNull("telphone")) {
                        result.telphone = json.optString("telphone", null);
                    }

                    if (!json.isNull("intro")) {
                        result.intro = json.optString("intro", null);
                    }

                    if (!json.isNull("intro")) {
                        result.intro = json.optString("intro", null);
                    }

                    if (!json.isNull("dest")) {
                        result.dest = json.optString("dest", null);
                    }

                    if (!json.isNull("accessToken")) {
                        result.accessToken = json.optString("accessToken", null);
                    }

                    if (!json.isNull("maritalStatus")) {
                        result.maritalStatus = json.optString("maritalStatus", null);
                    }

                    result.isFocus = json.optInt("focus") == 1 || json.optInt("isFocus") == 2;

                    result.isBlack = json.optInt("isBlack") == 1;

                }
            }
            return result;
        }
        return null;
    }
}
